/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application.externalServices;

/**
 *
 * @author aa6345 (based on example from http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html)
 */
import java.net.*;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class AlchemyTermExtractor {

    private String fullURL;
    private String fullText;
    private String fullResponse;
    private ArrayList<String> extractedKeywords = new ArrayList();

    public void AlchemyTermExtractor() {
        this.fullText = "";
        this.fullURL = "";
        this.fullResponse = "";
        //this.extractedKeywords = null;
    }

    public void AlchemyTermExtractor(String thisText) {
        /*thisURL has to be URL Encoded*/
        //public static void main(String[] args) throws Exception {

        this.setFullText(thisText);
        this.createURLWithText();
        this.extractedKeywords = null;
    }

    public void startTermExtract() {
        try {
            URL alchemy = new URL(this.getFullURL());
            URLConnection yc = alchemy.openConnection();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    yc.getInputStream()));

            String inputLine;
            String fullTmpResponse = "";
            String[] tmpt;

            while ((inputLine = in.readLine()) != null) {
                //System.out.println(inputLine + "\n");
                if (!inputLine.contains("<!--")) {
                    tmpt = inputLine.split("<!--");
                    fullTmpResponse = fullTmpResponse + tmpt[0];
                } else {
                    fullTmpResponse = fullTmpResponse + inputLine;
                }
            }
            this.setFullXMLResponse(fullTmpResponse);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void createURLWithText() {
        String part1 = "http://access.alchemyapi.com/calls/text/TextGetRankedConcepts?apikey=98e35fcf16703f58f6c0eeec723cd110a8736b0c&outputMode=xml&text=%22";
        String part2 = "%22";
        String encodedFullText = "";

        try {
            encodedFullText = URLEncoder.encode(this.getFullText().toLowerCase(), "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }        

        String encodedFullTextRefined="";
        if(encodedFullText.length() > 5000) {
            encodedFullTextRefined = encodedFullText.substring(0, 5000);
        } else {
            encodedFullTextRefined = encodedFullText;
        }

        this.setFullURL(part1 + encodedFullTextRefined + part2);
        System.out.println(this.getFullURL());
    }

    public String getFullText() {
        return fullText;
    }

    public void setFullText(String fullText) {
        this.fullText = fullText;
    }

    public String getFullURL() {
        return fullURL;
    }

    public void setFullURL(String fullURL) {
        this.fullURL = fullURL;
    }

    public String getFullXMLResponse() {
        return fullResponse;
    }

    public void setFullXMLResponse(String fullResponse) {
        this.fullResponse = fullResponse;
    }

    public void addToExtractedKeywords(String s) {
        this.extractedKeywords.add(s);
    }

    public ArrayList getExtractedKeywords() {
        return this.extractedKeywords;
    }

    public void setExtractedKeywords(ArrayList extractedKeywords) {
        this.extractedKeywords = extractedKeywords;
    }

    public void extractXMLKeywords() {
        try {

            DocumentBuilderFactory factory =
                    DocumentBuilderFactory.newInstance();
            DocumentBuilder db = factory.newDocumentBuilder();
            InputSource inStream = new InputSource();
            inStream.setCharacterStream(new StringReader(this.getFullXMLResponse()));
            Document doc = db.parse(inStream);

            doc.getDocumentElement().normalize();

            NodeList nodeLst = doc.getElementsByTagName("results");
            //NodeList nodeLst = doc.getElementsByTagName("query"); //used with old termextractor url

            ArrayList<String> tmpArrayList = new ArrayList<String>();

            for (int s = 0; s < nodeLst.getLength(); s++) {
                Node fstNode = nodeLst.item(s);
                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element fstElmnt = (Element) fstNode;
                    //NodeList resultsElmntLst = fstElmnt.getElementsByTagName("results"); //used with old termextractor url
                    NodeList resultsElmntLst = fstElmnt.getElementsByTagName("concepts");

                    for (int k = 0; k < resultsElmntLst.getLength(); k++) {

                        //used with old termextractor url
                        Node resultsNode = resultsElmntLst.item(k);
                        if (resultsNode.getNodeType() == Node.ELEMENT_NODE) {
                            Element resultsElmnt = (Element) resultsNode;
                            NodeList resultElmntLst = resultsElmnt.getElementsByTagName("concept");

                            for (int r = 0; r < resultElmntLst.getLength(); r++) {

                                Node resultsNode2 = resultElmntLst.item(r);
                                if (resultsNode2.getNodeType() == Node.ELEMENT_NODE) {
                                    Element resultsElmnt2 = (Element) resultsNode2;
                                    NodeList resultElmntLst2 = resultsElmnt2.getElementsByTagName("text");

                                    for (int m = 0; m < resultElmntLst2.getLength(); m++) {

                                        if (!tmpArrayList.contains(resultElmntLst2.item(m).getTextContent().trim())) {
                                            tmpArrayList.add(resultElmntLst2.item(m).getTextContent().trim());
                                        }
                                    }
                                }

                            }
                        }

                    }

                }
            }

            //this.setExtractedKeywords(tmpArrayList);
            this.getExtractedKeywords().addAll(tmpArrayList);

        } catch (Exception e) {
            //System.out.println(e);
        }

    }
}
